import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Cookies from 'vue-cookies'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/order/order1',
    component: Home,
    children: [
      {
        path: '/homePage',
        name: 'homePage',
        redirect: '/homePage/homePage1',
        component: () => import('../views/home/HomePage.vue'),
        children: [
          {
            path: '/homePage/homePage1',
            name: 'homePage1',
            component: () => import('../views/homePage/HomePage1.vue')
          },
          {
            path: '/homePage/homePage2',
            name: 'homePage2',
            component: () => import('../views/homePage/HomePage2.vue')
          },
          {
            path: '/homePage/homePage3',
            name: 'homePage3',
            component: () => import('../views/homePage/HomePage3.vue')
          },
        ]
      },
      {
        path: '/order',
        name: 'order',
        redirect: '/order/order1',
        component: () => import('../views/home/Order.vue'),
        children: [
          {
            path: '/order/order1',
            name: 'order1',
            component: () => import('../views/order/Order1.vue')
          },
          {
            path: '/order/order2',
            name: 'order2',
            component: () => import('../views/order/Order2.vue')
          },
          {
            path: '/order/order3',
            name: 'order3',
            component: () => import('../views/order/Order3.vue')
          },
        ]
      },
      {
        path: '/goods',
        name: 'goods',
        redirect: '/goods/goods1',
        component: () => import('../views/home/Goods.vue'),
        children: [
          {
            path: '/goods/goods1',
            name: 'goods1',
            component: () => import('../views/goods/Goods1.vue')
          },
          {
            path: '/goods/goods2',
            name: 'goods2',
            component: () => import('../views/goods/Goods2.vue')
          },
          {
            path: '/goods/goods3',
            name: 'goods3',
            component: () => import('../views/goods/Goods3.vue')
          },
          {
            path: '/goods/goods4',
            name: 'goods4',
            component: () => import('../views/goods/Goods4.vue')
          },
          {
            path: '/goods/goods5',
            name: 'goods5',
            component: () => import('../views/goods/Goods5.vue')
          },
        ]
      },
      {
        path: '/vip',
        name: 'VIP',
        redirect: '/vip/vip1',
        component: () => import('../views/home/VIP.vue'),
        children: [
          {
            path: '/vip/vip1',
            name: 'vip1',
            component: () => import('../views/vip/vip1.vue')
          },
          {
            path: '/vip/vip2',
            name: 'vip2',
            component: () => import('../views/vip/vip2.vue')
          },
        ]
      },
      {
        path: '/set',
        name: 'set',
        redirect: '/set/set1',
        component: () => import('../views/home/Set.vue'),
        children: [
          {
            path: '/set/set1',
            name: 'set1',
            component: () => import('../views/set/Set1.vue')
          },
          {
            path: '/set/set2',
            name: 'set2',
            component: () => import('../views/set/Set2.vue')
          },
          {
            path: '/set/set3',
            name: 'set3',
            component: () => import('../views/set/Set3.vue')
          },
          {
            path: '/set/set4',
            name: 'set4',
            component: () => import('../views/set/Set4.vue')
          },
        ]
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

var isLogin = Cookies.get('token');
router.beforeEach((to, from, next)=> {
  if(to.path == '/login') {
    next()
  }else {
    if(isLogin) {
      next()
    }else {
      next('/login')
    }
  }
})

export default router
